package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvUsername;
    private Button btnLogout;
    private LinearLayout btnAcceptance;
    private LinearLayout btnDelivery;
    private LinearLayout btnInventoryDetail;
    private LinearLayout btnOutventoryDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 设置返回按钮行为（使用OnBackPressedDispatcher兼容Android 16+）
        getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                // 禁用返回按钮，防止用户直接返回登录界面
                moveTaskToBack(true);
            }
        });

        // 初始化控件
        initViews();

        // 设置点击事件
        setListeners();

        // 从Intent获取用户名
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("username")) {
            String username = intent.getStringExtra("username");
            tvUsername.setText(username);
        }
    }

    private void initViews() {
        tvUsername = findViewById(R.id.tv_username);
        btnLogout = findViewById(R.id.btn_logout);
        btnAcceptance = findViewById(R.id.btn_acceptance);
        btnDelivery = findViewById(R.id.btn_delivery);
        btnInventoryDetail = findViewById(R.id.btn_inventory_detail);
        btnOutventoryDetail = findViewById(R.id.btn_outventory_detail);
    }

    private void setListeners() {
        // 退出登录按钮点击事件
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logout();
            }
        });

        // 食材验收按钮点击事件
        btnAcceptance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, OrderAcceptanceActivity.class);
                startActivity(intent);
            }
        });

        // 食材出库按钮点击事件
        btnDelivery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateToFunction("食材出库");
            }
        });

        // 入库详情按钮点击事件
        btnInventoryDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateToFunction("入库详情");
            }
        });

        // 出库详情按钮点击事件
        btnOutventoryDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateToFunction("出库详情");
            }
        });
    }

    private void logout() {
        // 清除登录状态
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    private void navigateToFunction(String functionName) {
        // 显示功能提示
        Toast.makeText(MainActivity.this, "您点击了" + functionName + "功能", Toast.LENGTH_SHORT).show();
        
        // 这里可以根据不同功能跳转到相应的Activity
        // 由于只是模拟，我们先使用Toast提示
    }

}